package com.hainatong.shiphelper.widget.recycleviewdriver;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import androidx.annotation.NonNull;
import android.view.View;
import androidx.recyclerview.widget.RecyclerView;

public class MyItemDecoration extends RecyclerView.ItemDecoration
{

    private int left;

    private int top;

    private int right;

    private int bottom;

    private int dividerColor;

    private int padingTop;

    private int padingBottom;

    private Paint mPaint;


    public MyItemDecoration(int left, int top, int right, int bottom)
    {
        this.left = left;
        this.top = top;
        this.right = right;
        this.bottom = bottom;
    }

    public MyItemDecoration(int left, int top, int right, int bottom , int dividerColor)
    {
        this.left = left;
        this.top = top;
        this.right = right;
        this.bottom = bottom;
        this.dividerColor = dividerColor;
        mPaint = new Paint();
        mPaint.setColor(dividerColor);
    }

    public int getPadingTop() {
        return padingTop;
    }

    public void setPadingTop(int padingTop) {
        this.padingTop = padingTop;
    }

    public int getPadingBottom() {
        return padingBottom;
    }

    public void setPadingBottom(int padingBottom) {
        this.padingBottom = padingBottom;
    }

    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view,
                               @NonNull RecyclerView parent, @NonNull RecyclerView.State state)
    {
        super.getItemOffsets(outRect, view, parent, state);
        if (left == 0 && top == 0 && right == 0 && bottom == 0)
        {
            return;
        }

        outRect.set(left, top, right, bottom);
    }

    @Override
    public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent,
                       @NonNull RecyclerView.State state)
    {
        super.onDraw(c, parent, state);

    }

    @Override
    public void onDrawOver(@NonNull Canvas c, @NonNull RecyclerView parent,
                           @NonNull RecyclerView.State state)
    {
        super.onDrawOver(c, parent, state);
        if (mPaint == null)
        {
            return;
        }
        // 获取RecyclerView的child view的个数
        int childCount = parent.getChildCount();
        // 遍历每个Item，分别获取它们的位置信息，然后再绘制对应的分割线
        for (int i = 0; i < childCount; i++)
        {
            // 获取每个Item
            View child = parent.getChildAt(i);
            int mleft;
            if( i == 0 )
            {
                 mleft = child.getLeft() + padingTop;
            }
            else
            {
                mleft = child.getLeft();
            }


            // 需要加上 margin的高度
            final int top = child.getBottom();
            int mright;
            if( i == childCount -1)
            {
               mright = child.getRight() + padingBottom;
            }
            else
            {
                mright = child.getRight();
            }

            final int bottom = top ;
            // 绘制分割线
            c.drawRect(mleft, top, mright, bottom, mPaint);
        }
    }
}
